﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessCommon.Utilities;
using StoreWinform.Controller;
using BusinessCommon.DTO;
using System.Threading;
using CustomControlLibrary;

namespace StoreWinform.View
{
    public partial class ChangePassForm : Form
    {
        private const string COMMON = "Common";
        ChangePassFormController control;
        StmProcessBar stmProcessBar = StmProcessBar.Instance;
        UserDto oldUserInfo;

        public ChangePassForm()
        {
            oldUserInfo = (UserDto)SessionUtilites.GetSession(SessionUtilites.SESSION_KEY);
            InitializeComponent();
            SetLanguage();
            HideMessage();
        }

        /// <summary>
        /// Hide message box
        /// </summary>
        private void HideMessage()
        {
            splitContainer.Panel1Collapsed = true;
            splitContainer.Panel1.Hide();
        }

        /// <summary>
        /// Set language
        /// </summary>
        private void SetLanguage()
        {
            btnCloseMsgBox.setMultiLanguage(this.Name, btnCloseMsgBox.Name);
            btnCancel.setMultiLanguage(this.Name, btnCancel.Name);
            btnUpdate.setMultiLanguage(this.Name, btnUpdate.Name);
            lblPassword.setMultiLanguage(this.Name, lblPassword.Name);
            lblPasswordConfirm.setMultiLanguage(this.Name, lblPasswordConfirm.Name);
            this.Text = ResxManagerString.GetPropertyString(this.Name, this.Name);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string errMsg = ValidateUpdate();
            bool success = false;
            if (!StringUtilities.IsNull(errMsg))
            {
                List<string> lstMessageErr = new List<string>();
                lstMessageErr.Add(errMsg);
                splitContainer.Panel1Collapsed = false;
                splitContainer.Panel1.Show();
                smMessageListBox.ShowMessage(lstMessageErr, SmEnum.MSG_BOX_TYPE.ERR);
            }
            else
            {
                Thread backgroundThread = new Thread(
                new ThreadStart(() =>
                {
                    // Update action
                    if (control.UpdateData(txtPassword.Text, oldUserInfo.Password, oldUserInfo.User_ID))
                    {
                        success = true;
                    }
                    stmProcessBar.Close();
                }
                ));
                backgroundThread.Start();
                backgroundThread.Join();
                if (success)
                {
                    this.Visible = false;
                    MenuController.Instance.GetView.Visible = true;
                }
            }

        }

        private string ValidateUpdate()
        {
            UserDto oldUserInfo = (UserDto)SessionUtilites.GetSession(SessionUtilites.SESSION_KEY);
            string errMessage = null;
            if (!txtPassword.Text.Equals(txtPasswordConfirm.Text))
            {
                string[] paramError = new string[5];
                paramError[0] = lblPassword.Text;
                paramError[1] = lblPasswordConfirm.Text;
                errMessage = ErrorProcessor.CreateMessageError(ItemDefineIF.ERR_00004, paramError);
                return errMessage;
            }
            if (txtPassword.Text.Length < 6)
            {
                string[] paramError = new string[5];
                paramError[0] = lblPassword.Text;
                paramError[1] = ResxManagerString.GetPropertyString(this.Name, "errLengthCheck");
                errMessage = ErrorProcessor.CreateMessageError(ItemDefineIF.ERR_00007, paramError);
                return errMessage;
            }
            string newPassword = txtPassword.Text;
            if (oldUserInfo.Password.Equals(txtPassword.Text))
            {
                string[] paramError = new string[5];
                paramError[0] = lblPassword.Text;
                paramError[1] = lblPasswordConfirm.Text;
                errMessage = ErrorProcessor.CreateMessageError(ItemDefineIF.ERR_00006, paramError);
                return errMessage;
            }
            return errMessage;
        }

        /// <summary>
        /// Validate required
        /// </summary>
        private void validateRequired()
        {
            if (!String.IsNullOrEmpty(txtPassword.Text)
                && !String.IsNullOrEmpty(txtPasswordConfirm.Text))
            {
                btnUpdate.Enabled = true;
            }
            else
            {
                btnUpdate.Enabled = false;
            }
        }

        /// <summary>
        /// Show message box
        /// </summary>
        /// <param name="errCode">errCode</param>
        /// <param name="listBoxType">Msg box type</param>
        public void ShowMessage(string errCode, SmEnum.MSG_BOX_TYPE listBoxType)
        {
            List<string> listMessageErr = new List<string>();

            if (!String.IsNullOrEmpty(errCode))
            {
                string messageErr = "";
                string[] paramError = new string[5];
                if (errCode.Equals(ItemDefineIF.ERR_00011))
                {
                    paramError[0] = ResxManagerString.GetPropertyString("accountName", COMMON);
                    paramError[1] = ResxManagerString.GetPropertyString("Update", COMMON);
                    messageErr = ErrorProcessor.CreateMessageError(errCode, paramError);
                }
                else
                {
                    paramError = new string[5];
                    messageErr = ErrorProcessor.CreateMessageError(errCode, paramError);
                }
                listMessageErr.Add(messageErr);
            }
            splitContainer.Panel1Collapsed = false;
            splitContainer.Panel1.Show();
            smMessageListBox.ShowMessage(listMessageErr, listBoxType);
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            validateRequired();
        }

        private void txtPasswordConfirm_TextChanged(object sender, EventArgs e)
        {
            validateRequired();
        }

        public void SetController(ChangePassFormController changePassFormController)
        {
            control = changePassFormController;
        }

        private void btnCloseMsgBox_Click_1(object sender, EventArgs e)
        {
            HideMessage();
        }
    }
}
